package cn.enilu.flash.mobile.controller;

import cn.enilu.flash.bean.constant.factory.PageFactory;
import cn.enilu.flash.bean.entity.bus.Repair;
import cn.enilu.flash.bean.entity.bus.Rooms;
import cn.enilu.flash.bean.entity.bus.RoomsAppointment;
import cn.enilu.flash.bean.enumeration.bus.Pay;
import cn.enilu.flash.bean.enumeration.bus.RoomsState;
import cn.enilu.flash.bean.vo.front.Rets;
import cn.enilu.flash.bean.vo.query.SearchFilter;
import cn.enilu.flash.mobile.domain.RoomsAppointmentDomain;
import cn.enilu.flash.service.bus.RoomsAppointmentService;
import cn.enilu.flash.service.bus.RoomsService;
import cn.enilu.flash.utils.DateUtil;
import cn.enilu.flash.utils.factory.Page;
import cn.enilu.flash.web.controller.BaseController;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Stream;


@RestController
@RequestMapping("/rooms")
public class RoomsController extends BaseController {

    @Autowired
    private RoomsService roomsService;

    @Autowired
    private RoomsAppointmentService roomsAppointmentService;

    @GetMapping(value = "/list")
    public Object list() {
        Page<Rooms> page = new PageFactory<Rooms>().defaultPage();
        page.addFilter(SearchFilter.build("state", SearchFilter.Operator.EQ, "1"));
        page = roomsService.queryPage(page);
        return Rets.success(page);
    }

    @GetMapping(value = "/{id}")
    public Object getInfoById(@PathVariable("id") Long id) {
        Rooms rooms = roomsService.get(id);
        return Rets.success(rooms);
    }

    @GetMapping(value = "/appoint/list")
    public Object appointList() {
        Long idUser = getIdUser();
        Page<RoomsAppointment> page = new PageFactory<RoomsAppointment>().defaultPage();
        page.addFilter(SearchFilter.build("userId", SearchFilter.Operator.EQ,  idUser));
        page.setSort(Sort.by(Sort.Direction.DESC, "createTime"));
        page = roomsAppointmentService.queryPage(page);
        return Rets.success(page);
    }

    @PostMapping("/appoint")
    public Object appoint(@RequestBody RoomsAppointmentDomain roomsAppointmentDomain) {
        Long idUser = getIdUser();
        if (null != idUser) {
            List<RoomsAppointment> roomsAppointments = new ArrayList<>();
            List<Date> times = roomsAppointmentDomain.getTimes();
            Collections.sort(times);
            RoomsAppointment roomsAppointment = new RoomsAppointment();
            roomsAppointment.setUserId(idUser);
            roomsAppointment.setCreateBy(idUser);
            roomsAppointment.setModifyBy(idUser);
            roomsAppointment.setAppointStart(times.get(0));
            roomsAppointment.setAppointEnd(times.get(times.size()-1));
            roomsAppointment.setRoomId(roomsAppointmentDomain.getRoomId());
            roomsAppointment.setState(RoomsState.APPOINTED.getCol());
            roomsAppointment.setPayment(Pay.PAYED.getCol());
            roomsAppointments.add(roomsAppointment);
            roomsAppointmentService.insertAll(roomsAppointments);
            return Rets.success("预约成功");
        } else {
            return Rets.failure("预约请登录");
        }
    }
}
